#include <iostream>
using namespace std;
const int N = 1e6 + 10;

int p[N],size[N];
int find(int x) //查找x的祖宗节点 + 路径压缩
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0 ; i < n ; ++i)
    {
        p[i] = i;
        size[i] = 1;
    }
    
    while(m--)
    {
        int op,x,y;
        scanf("%d",&op);
        if(op == 1)
        {
            scanf("%d%d",&x,&y);
            //合并x对应集合和y对应集合
            if(find(x) == find(y)) continue;
            size[find(y)] += find(x);
            p[find(x)] = find(y);
        }
        else if(op == 2)
        {
            //查询x和y是否在同一个集合
            scanf("%d%d",&x,&y);
            if(find(x) == find(y)) printf("Y\n");
            else printf("N\n");
        }
        else if(op == 3)
        {
            //查看x对应集合的数量
            scanf("%d",&x);
            printf("%d\n",size[find(x)]);
        }
    }
    return 0;
}